package com.quyunshuo.module.home.room.manager

import com.quyunshuo.module.home.room.dao.ConcentrationDao
import com.quyunshuo.module.home.room.dao.PoetDao
import com.quyunshuo.module.home.room.entity.ConcentrationEntity
import com.quyunshuo.module.home.room.entity.PoetEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext

class ConcentrationRepository(private val concentrationDao: ConcentrationDao) {
    // 获取所有诗人
    val allPoets: Flow<List<ConcentrationEntity>> = concentrationDao.getAllConcentration()


    // 按ID获取诗人
    fun getPoetById(id: Long): Flow<ConcentrationEntity> {
        return concentrationDao.getConcentrationById(id)
    }

    // 插入诗人
    suspend fun insert(poet: ConcentrationEntity) {
        withContext(Dispatchers.IO) {
            concentrationDao.insert(poet)
        }
    }

    // 插入所有诗人
    suspend fun insertAll(poets: List<ConcentrationEntity>) {
        withContext(Dispatchers.IO) {
            concentrationDao.insertAll(poets)
        }
    }

    suspend fun getIdAndPoetList(): MutableMap<Long, ConcentrationEntity> {
        return withContext(Dispatchers.IO) {
            val list = concentrationDao.getAll()
            list.associateBy { it.id }
        }.toMutableMap()//todo 是否需要可变？看你需要不需要修改
    }


    // 删除所有诗人
    suspend fun deleteAll() {
        withContext(Dispatchers.IO) {
            concentrationDao.deleteAll()
        }
    }
}